{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 页面访问与开通意愿分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.feature_selection import SelectFromModel\n",
    "from sklearn.metrics import accuracy_score,precision_score,recall_score,confusion_matrix\n",
    "from operator import itemgetter, attrgetter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>uid</th>\n",
       "      <th>category</th>\n",
       "      <th>pvcount</th>\n",
       "      <th>pass</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00008391</td>\n",
       "      <td>ACT</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>00008391</td>\n",
       "      <td>BBZ-Native</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>00008391</td>\n",
       "      <td>Bus</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00008391</td>\n",
       "      <td>Carrent</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>00008391</td>\n",
       "      <td>CouponCodeHybird</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        uid          category  pvcount  pass\n",
       "0  00008391               ACT       29     0\n",
       "1  00008391        BBZ-Native       12     0\n",
       "2  00008391               Bus        1     0\n",
       "3  00008391           Carrent       30     0\n",
       "4  00008391  CouponCodeHybird        1     0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df0 = pd.read_csv('D:\\\\QIAN\\\\CODE\\\\oschina\\\\pydata\\\\export_20170718.txt',sep='\\t')\n",
    "df0.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原始数据：前七天的页面分类访问量和在开通引导页点击申请的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ACT</th>\n",
       "      <th>AI Lee</th>\n",
       "      <th>AdvertisementH5</th>\n",
       "      <th>AdvertisementHybird</th>\n",
       "      <th>BBZ-Native</th>\n",
       "      <th>Bus</th>\n",
       "      <th>Carrent</th>\n",
       "      <th>CouponCodeHybird</th>\n",
       "      <th>Credit-Hybrid</th>\n",
       "      <th>Credit1</th>\n",
       "      <th>...</th>\n",
       "      <th>vbk_app</th>\n",
       "      <th>video_h5</th>\n",
       "      <th>video_hybrid</th>\n",
       "      <th>video_native</th>\n",
       "      <th>view</th>\n",
       "      <th>vipcard-Hybrid</th>\n",
       "      <th>vipwelfare-H5</th>\n",
       "      <th>widget-hybrid</th>\n",
       "      <th>yfi</th>\n",
       "      <th>pass</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>uid</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>00008391</th>\n",
       "      <td>29.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00156590</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00257166</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>01111730</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>01165210</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 141 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           ACT  AI Lee  AdvertisementH5  AdvertisementHybird  BBZ-Native  Bus  \\\n",
       "uid                                                                             \n",
       "00008391  29.0     0.0              0.0                  0.0        12.0  1.0   \n",
       "00156590   0.0     0.0              0.0                  0.0         0.0  1.0   \n",
       "00257166   0.0     0.0              0.0                  0.0         0.0  0.0   \n",
       "01111730   0.0     0.0              0.0                  0.0         0.0  6.0   \n",
       "01165210   0.0     0.0              0.0                  0.0         0.0  0.0   \n",
       "\n",
       "          Carrent  CouponCodeHybird  Credit-Hybrid  Credit1  ...   vbk_app  \\\n",
       "uid                                                          ...             \n",
       "00008391     30.0               1.0            0.0      0.0  ...       0.0   \n",
       "00156590      2.0               0.0            0.0      0.0  ...       0.0   \n",
       "00257166      2.0               0.0            0.0      0.0  ...       0.0   \n",
       "01111730     79.0              24.0            0.0      0.0  ...       0.0   \n",
       "01165210     20.0               0.0            0.0      0.0  ...       0.0   \n",
       "\n",
       "          video_h5  video_hybrid  video_native  view  vipcard-Hybrid  \\\n",
       "uid                                                                    \n",
       "00008391       0.0           0.0           0.0   0.0             0.0   \n",
       "00156590       0.0           0.0           0.0   0.0             0.0   \n",
       "00257166       0.0           0.0           0.0   0.0             0.0   \n",
       "01111730       0.0           0.0           0.0   0.0             0.0   \n",
       "01165210       0.0           0.0           0.0   0.0             0.0   \n",
       "\n",
       "          vipwelfare-H5  widget-hybrid  yfi  pass  \n",
       "uid                                                \n",
       "00008391            0.0            0.0  0.0     0  \n",
       "00156590            0.0            0.0  1.0     1  \n",
       "00257166            0.0            0.0  1.0     0  \n",
       "01111730            0.0            0.0  0.0     0  \n",
       "01165210            0.0            0.0  0.0     0  \n",
       "\n",
       "[5 rows x 141 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = df0.pivot(index='uid', columns='category', values='pvcount')\n",
    "df2 = df0[['uid','pass']].drop_duplicates().set_index('uid')\n",
    "\n",
    "df = df1.join(df2)\n",
    "df = df.fillna(0)\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据重整为按页面分类为维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "features = df.columns[:df1.columns.size]\n",
    "\n",
    "train_X, test_X, train_y, test_y = train_test_split(df[features], df['pass'], test_size=0.25, random_state=0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分割数据集，训练集和测试集的比例为75:25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
       "            min_impurity_split=1e-07, min_samples_leaf=1,\n",
       "            min_samples_split=2, min_weight_fraction_leaf=0.0,\n",
       "            n_estimators=10, n_jobs=2, oob_score=False, random_state=None,\n",
       "            verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf = RandomForestClassifier(n_jobs=2)\n",
    "clf.fit(train_X, train_y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('home', 0.072390257818602866)\n",
      "('Train', 0.068589698070320101)\n",
      "('Widget', 0.062527879679425982)\n",
      "('Inland_Hotel', 0.059026691147471)\n",
      "('Customer', 0.056198038264681907)\n",
      "('Inland_Flight', 0.054862783889415144)\n",
      "('flightRN', 0.042560986454900682)\n",
      "('Travel_route', 0.040085929896213304)\n",
      "('Other', 0.037795995349663096)\n",
      "('global_search', 0.03148672982286236)\n"
     ]
    }
   ],
   "source": [
    "for feature in sorted(zip(features, clf.feature_importances_),key=itemgetter(1),reverse=True)[:10]:\n",
    "    print(feature)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "10个最重要维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "preds      0    1\n",
      "actual           \n",
      "0       3801  502\n",
      "1       1740  367\n"
     ]
    }
   ],
   "source": [
    "preds = clf.predict(test_X)\n",
    "\n",
    "print(pd.crosstab(test_y, preds, rownames=['actual'], colnames=['preds']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.65023400936\n"
     ]
    }
   ],
   "source": [
    "print(accuracy_score(test_y, preds))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "准确率为65"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.174181300427\n"
     ]
    }
   ],
   "source": [
    "print(recall_score(test_y,preds))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "召回率17，比较低。下一步将尝试修改维度如直接用页面而非分类，增加其他维度等方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
